package com.sks.service.base.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sks.bean.pojo.SysInfo;
import com.sks.dao.base.SysInfoDao;
import com.sks.service.base.BaseService;
import com.sks.service.base.SysInfoService;
import com.sks.utils.StringUtil;

@Service
@Transactional
public class SysInfoServiceBean extends BaseService<SysInfo> implements SysInfoService {

	@Resource(name = "sysInfoDao")
	public void setSysInfoDao(SysInfoDao sysInfoDao) {
		this.setDao(sysInfoDao);
	}
	
	@Override
	public SysInfo get(String key) {
	   try {
         String jpql = "select o from SysInfo o where o.field = ?";
         return (SysInfo) this.getObjectByJpql(jpql, new Object[]{key});
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
	
	@Override
	public String getString(String key) {
	   try {
         SysInfo si = this.get(key);
         if (null != si) {
            return si.getValue();
         }
         return null;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
	
	@Override
	public Boolean getBoolean(String key) {
	   return getBoolean(key, null);
	}
	
	@Override
	public Boolean getBoolean(String key, Boolean defaultValue) {
	   try {
         SysInfo si = this.get(key);
         if (null != si) {
            return StringUtil.toBoolean(si.getValue(), defaultValue);
         }
         return null;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
	
	@Override
	public Integer getInteger(String key) {
	   return getInteger(key, null);
	}
	
	@Override
	public Integer getInteger(String key, Integer defaultValue) {
	   try {
         SysInfo si = this.get(key);
         if (null != si) {
            return StringUtil.toInteger(si.getValue(), defaultValue);
         }
         return null;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
	
	@Override
	public Double getDouble(String key) {
	   return getDouble(key, null);
	}
	
	@Override
	public Double getDouble(String key, Double defaultValue) {
	   try {
         SysInfo si = this.get(key);
         if (null != si) {
            return StringUtil.toDouble(si.getValue(), defaultValue);
         }
         return null;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
	
	@Override
	public Long getLong(String key) {
	   return getLong(key, null);
	}
	
	@Override
	public Long getLong(String key, Long defaultValue) {
	   try {
         SysInfo si = this.get(key);
         if (null != si) {
            return StringUtil.toLong(si.getValue(), defaultValue);
         }
         return null;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
	}
}
